package cn.itsource.ymcc.service.impl;

import cn.itsource.ymcc.constants.BaseConstants;
import cn.itsource.ymcc.domain.CourseOrder;
import cn.itsource.ymcc.domain.PayOrder;
import cn.itsource.ymcc.mapper.PayOrderMapper;
import cn.itsource.ymcc.service.IPayOrderService;
import cn.itsource.ymcc.service.IPayService;
import cn.itsource.ymcc.to.PlaceCourseOrderTo;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author whale
 * @since 2021-09-25
 */
@Service
@Slf4j
public class PayOrderServiceImpl extends ServiceImpl<PayOrderMapper, PayOrder> implements IPayOrderService {

    @Autowired
    private PayOrderMapper payOrderMapper ;

    @Autowired
    private IPayService payService;

    @Override
    public PayOrder selectByOrderNo(String orderNo) {
        return payOrderMapper.selectByOrderNo(orderNo);
    }

    @Override
    public void savePayOrder(PlaceCourseOrderTo to) {
        PayOrder payOrder = new PayOrder();
        payOrder.setSubject(to.getSubject());
        payOrder.setRelationId(to.getRelationId());
        payOrder.setOrderNo(to.getOrderNo());
        payOrder.setAmount(to.getAmount());
        payOrder.setCreateTime(new Date());
        payOrder.setPayType(to.getPayType());
        payOrder.setUserId(to.getUserId());
        payOrder.setExtParams(to.getExtParams());
        payOrder.setPayStatus(BaseConstants.Pay.STATUS_APPLY);
        insert(payOrder);
    }

    @Override
    public PayOrder savePayOrder(CourseOrder courseOrder) {
        PayOrder payOrder = new PayOrder();
        payOrder.setSubject(courseOrder.getTitle());
        payOrder.setRelationId(courseOrder.getId());
        payOrder.setOrderNo(courseOrder.getOrderNo());
        payOrder.setAmount(courseOrder.getTotalAmount());
        payOrder.setCreateTime(new Date());
        payOrder.setPayType(courseOrder.getPayType());
        payOrder.setUserId(courseOrder.getUserId());
        List<Long> courseIds = courseOrder.getItems().stream().map(i -> i.getId()).collect(Collectors.toList());
        payOrder.setExtParams("userId="+ courseOrder.getUserId()+"&relationId="+StringUtils.join(courseIds,","));
        payOrder.setPayStatus(BaseConstants.Pay.STATUS_APPLY);
        insert(payOrder);
        return payOrder;
    }

    @Override
    public void closeOrder(PlaceCourseOrderTo placeCourseOrderTo) {
        synchronized (placeCourseOrderTo.getOrderNo()){
            PayOrder payOrder = payOrderMapper.selectByOrderNo(placeCourseOrderTo.getOrderNo());
            if(payOrder.getPayStatus() == BaseConstants.Pay.STATUS_APPLY){
                log.info("支付超时关单，修改支付单状态...");
                payOrder.setPayStatus(BaseConstants.Pay.STATUS_OUT_TIME);
                updateById(payOrder);
                //调用支付宝关单接口
                payService.closeOrder(placeCourseOrderTo);
            }
        }
    }

}
